package dao

import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
 * Created by yz on 2018/4/17
 */
class SampleDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  type MyRow = SampleRow
  val table = Sample

  def selectBySampleName(userId: Int, sampleName: String) = db.run(table.
    filter(_.userId === userId).filter(_.sampleName === sampleName).result.headOption)

  def insert(row: MyRow): Future[Unit] = db.run(table += row).map(_ => ())

  def selectAll(userId: Int) = db.run(table.filter(_.userId === userId).
    sortBy(_.id.desc).result)

  def selectAll(ids: List[Int]) = db.run(table.
    filter(_.id.inSetBind(ids)).result)

  def deleteById(id: Int): Future[Unit] = db.run(table.filter(_.id === id).delete).map(_ => ())

  def deletes(ids: List[Int]) = db.run(table.filter(_.id.inSetBind(ids)).delete)

  def deleteByUserId(userId: Int): Future[Unit] = db.run(table.filter(_.userId === userId).delete).map(_ => ())

  def selectById(id: Int) = db.run(table.
    filter(_.id === id).result.head)

  def update(row: MyRow): Future[Unit] = db.run(table.filter(_.id === row.id).
    update(row)).map(_ => ())

  def create = db.run(table.schema.create)


}
